﻿/*
 *   Copyright 2011 Peter Crossley
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 *  ----------------------------------------------------------------------------
 *  File:       CloudServiceManager.cs
 *  Author:     Peter Crossley
 *  ----------------------------------------------------------------------------
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UEC;
using UEC.Modules;
namespace UEC.ConfigUI
{
    public partial class CloudServiceManager : Form
    {
        private CloudManager manager;
        public CloudServiceManager(CloudManager manager)
        {
            this.manager = manager;
            InitializeComponent();
            manager.LoadUserDataSettings();
            generateServiceUI();
            this.manager.SettingsChangeEvent += new CloudManager.SettingsChangedHandler(SettingsChanged);
            this.manager.SaveSettingsEvent += new CloudManager.SaveSettingsHandler(SettingsSaved);
            this.manager.CloudManagerEvent += new CloudManager.CloudManagerHandler(CloudManagerState);
         }

        

        private void generateServiceUI()
        {
            IEnumerable<ICloudModule> orderedUI = manager.Services.OrderByDescending(x => x, new SpecialComparer());
            int serviceIndex = 0;
            foreach (ICloudModule service in orderedUI)
            {
                TabPage page = new System.Windows.Forms.TabPage();
                page.Name = "tabPageService" + serviceIndex;
                page.Padding = new System.Windows.Forms.Padding(3);
                page.Size = new System.Drawing.Size(422, 320);
                page.TabIndex = serviceIndex++;
                page.Text = service.GetServiceName();
                page.UseVisualStyleBackColor = true;
                page.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                | System.Windows.Forms.AnchorStyles.Left)
                | System.Windows.Forms.AnchorStyles.Right)));
                UserControl control = service.GetConfigurationControl();
                control.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                | System.Windows.Forms.AnchorStyles.Left)
                | System.Windows.Forms.AnchorStyles.Right)));
                page.Controls.Add(control);
                tabControl.Controls.Add(page);
            }

            //add the cloud settings tab last
            TabPage tabPage = new System.Windows.Forms.TabPage();
            tabPage.Name = "tabPageService" + serviceIndex;
            tabPage.Padding = new System.Windows.Forms.Padding(3);
            tabPage.Size = new System.Drawing.Size(422, 320);
            tabPage.TabIndex = serviceIndex++;
            tabPage.Text = Properties.Resources.uiUECSettingsTitle;
            tabPage.UseVisualStyleBackColor = true;
            tabPage.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));

            CloudSettings uecSettings = new CloudSettings(manager);
            uecSettings.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                | System.Windows.Forms.AnchorStyles.Left)
                | System.Windows.Forms.AnchorStyles.Right)));
            tabPage.Controls.Add(uecSettings);
            tabControl.Controls.Add(tabPage);
        }

        private class SpecialComparer : IComparer<ICloudModule>
        {
            public int Compare(ICloudModule d1, ICloudModule d2)
            {
                decimal fractional1, fractional2;

                try
                {
                    fractional1 = decimal.Remainder(d1.GetWeight(), decimal.Floor(d1.GetWeight()));
                }
                catch (DivideByZeroException)
                {
                    fractional1 = d1.GetWeight();
                }
                try
                {
                    fractional2 = decimal.Remainder(d2.GetWeight(), decimal.Floor(d2.GetWeight()));
                }
                catch (DivideByZeroException)
                {
                    fractional2 = d2.GetWeight();
                }

                if (fractional1 == fractional2)
                    return Decimal.Compare(Decimal.Floor(d1.GetWeight()), Decimal.Floor(d2.GetWeight()));
                else if (fractional1 > fractional2)
                    return 1;
                else
                    return -1;
            }
        }

        private void SettingsChanged(object sender, EventArgs e)
        {
            btnApply.Enabled = true;
        }

        private void SettingsSaved(object sender, EventArgs e)
        {
            btnApply.Enabled = false;
        }


        private void CloudManagerState(object sender, EventArgs e)
        {
            Close();
        }
        
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            manager.OnSaveSettings();
            Close();
        }

        private void lnkAbout_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            new AboutBox().ShowDialog();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            manager.OnSaveSettings();
        }
    }

}
